/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Project2.cs342.project1.mathNotebook.data;

import java.io.File;
import java.math.*;

/**
 *
 * @author Rick
 */
public class CompressionEncryptionDriver {

    /**
     * produces a OutOfMemoryError
     *
     *
     * @param args
     */
    public static void main(String[] args){
        /*This chunk of code compresses/encrypts local files.  The LocalEncryptionReader/Writer classes retrieve the RSA keys from the User class*/
        NotebookZip zip = new NotebookZip();
        zip.compressFile("test.xml");
        //File file = new File("test.gzip");
        RSAKeyGenerator rsaKeyGenerator = new RSAKeyGenerator();
        rsaKeyGenerator.generateKeys();
        User user = new User();
        user.setRsaPublicKey(rsaKeyGenerator.getPublicKey());
        user.setRsaPrivateKey(rsaKeyGenerator.getPrivateKey());
        LocalEncryptionWriter encryptedFile = new LocalEncryptionWriter(user, "password"); //"password" is just for testing.  Usually it will be user.getDESDefaultKey() or something.
        boolean writeSuccess = encryptedFile.writeEncryptedFile("test.gzip"); //Changes test.gzip to test2.encrypt so we can compare the results.
        System.out.println(writeSuccess);
        if(writeSuccess){
            BigInteger encryptedDES = encryptedFile.getEncryptedSecretKey();
            LocalEncryptionReader decryptedFile = new LocalEncryptionReader(user, encryptedDES);
            boolean readSuccess = decryptedFile.readEncryptedFile("test2.encrypt");
            System.out.println(readSuccess);
            if(readSuccess){
                zip.decompressFile("test2.gzip");    
            }
        }
        /*End Code that tests local compression/encryption*/

        /*This chunk of code compresses/encrypts files that will be turned into blobs and put on the database.  The DatabaseEncryptionReader/Writer classes retrieve the RSA keys from the Group class*/
        NotebookZip zip_Database = new NotebookZip();
        zip_Database.compressFile("test_Database.xml");
        //File file = new File("test.gzip");
        RSAKeyGenerator rsaKeyGenerator_Database = new RSAKeyGenerator();
        rsaKeyGenerator_Database.generateKeys();
        Group group = new Group();
        group.setRsaPublicKey(rsaKeyGenerator_Database.getPublicKey());
        group.setRsaPrivateKey(rsaKeyGenerator_Database.getPrivateKey());
        DatabaseEncryptionWriter encryptedFile_Database = new DatabaseEncryptionWriter(group, "password"); //"password" is just for testing.  Usually it will be user.getDESDefaultKey() or something.
        boolean writeSuccess_Database = encryptedFile_Database.writeEncryptedFile("test_Database.gzip"); //Changes test_Database.gzip to test_Database2.encrypt so we can compare the results.
        System.out.println(writeSuccess_Database);
        if(writeSuccess_Database){
            BigInteger encryptedDES_Database = encryptedFile_Database.getEncryptedSecretKey();
            DatabaseEncryptionReader decryptedFile_Database = new DatabaseEncryptionReader(group, encryptedDES_Database);
            boolean readSuccess_Database = decryptedFile_Database.readEncryptedFile("test_Database2.encrypt");
            System.out.println(readSuccess_Database);
            if(readSuccess_Database){
                zip_Database.decompressFile("test_Database2.gzip");
            }
        }
        /*End Code that tests To-Database compression/encryption*/
    }

}
